home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1BA4MM3 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  17.5 KB  |  531 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.Action;
  4. import com.sun.java.swing.CellRendererPane;
  5. import com.sun.java.swing.JComponent;
  6. import com.sun.java.swing.JScrollPane;
  7. import com.sun.java.swing.JTable;
  8. import com.sun.java.swing.JTextField;
  9. import com.sun.java.swing.JViewport;
  10. import com.sun.java.swing.KeyStroke;
  11. import com.sun.java.swing.ListSelectionModel;
  12. import com.sun.java.swing.LookAndFeel;
  13. import com.sun.java.swing.SwingUtilities;
  14. import com.sun.java.swing.UIManager;
  15. import com.sun.java.swing.plaf.ComponentUI;
  16. import com.sun.java.swing.plaf.TableUI;
  17. import com.sun.java.swing.plaf.UIResource;
  18. import com.sun.java.swing.table.JTableHeader;
  19. import com.sun.java.swing.table.TableCellEditor;
  20. import com.sun.java.swing.table.TableCellRenderer;
  21. import com.sun.java.swing.table.TableColumn;
  22. import com.sun.java.swing.text.JTextComponent;
  23. import com.sun.java.swing.text.Keymap;
  24. import java.awt.Color;
  25. import java.awt.Component;
  26. import java.awt.Container;
  27. import java.awt.Dimension;
  28. import java.awt.Graphics;
  29. import java.awt.Point;
  30. import java.awt.Rectangle;
  31. import java.awt.event.ActionEvent;
  32. import java.awt.event.FocusEvent;
  33. import java.awt.event.FocusListener;
  34. import java.awt.event.InputEvent;
  35. import java.awt.event.KeyEvent;
  36. import java.awt.event.KeyListener;
  37. import java.awt.event.MouseEvent;
  38. import java.awt.event.MouseListener;
  39. import java.awt.event.MouseMotionListener;
  40. import java.io.Serializable;
  41. import java.util.Enumeration;
  42.  
  43. public class BasicTableUI extends TableUI implements MouseListener, MouseMotionListener, FocusListener, Serializable {
  44.    protected JTable table;
  45.    private boolean phantomMousePressed = false;
  46.    private KeyListener tableKeyListener;
  47.    protected transient Component dispatchComponent;
  48.    protected CellRendererPane rendererPane;
  49.  
  50.    private void clearSelection() {
  51.       this.clearSelection(this.table.getColumnModel().getSelectionModel());
  52.       this.clearSelection(this.table.getSelectionModel());
  53.    }
  54.  
  55.    private void clearSelection(ListSelectionModel sm) {
  56.       sm.removeSelectionInterval(sm.getMinSelectionIndex(), sm.getMaxSelectionIndex());
  57.    }
  58.  
  59.    private void configureTable() {
  60.       LookAndFeel.installColorsAndFont(this.table, "Table.background", "Table.foreground", "Table.font");
  61.       Color sbg = this.table.getSelectionBackground();
  62.       if (sbg == null || sbg instanceof UIResource) {
  63.          this.table.setSelectionBackground(UIManager.getColor("Table.selectionBackground"));
  64.       }
  65.  
  66.       Color sfg = this.table.getSelectionForeground();
  67.       if (sfg == null || sfg instanceof UIResource) {
  68.          this.table.setSelectionForeground(UIManager.getColor("Table.selectionForeground"));
  69.       }
  70.  
  71.       Color gridColor = this.table.getGridColor();
  72.       if (gridColor == null || gridColor instanceof UIResource) {
  73.          this.table.setGridColor(UIManager.getColor("Table.gridColor"));
  74.       }
  75.  
  76.       Container parent = this.table.getParent();
  77.       if (parent != null) {
  78.          parent = ((Component)parent).getParent();
  79.          if (parent != null && parent instanceof JScrollPane) {
  80.             LookAndFeel.installBorder((JScrollPane)parent, "Table.scrollPaneBorder");
  81.          }
  82.       }
  83.  
  84.    }
  85.  
  86.    public static ComponentUI createUI(JComponent c) {
  87.       return new BasicTableUI();
  88.    }
  89.  
  90.    private void dispatchKeyboardEvent(KeyEvent e) {
  91.       int selectedRow = this.table.getSelectedRow();
  92.       int selectedColumn = this.table.getSelectedColumn();
  93.       if (selectedRow != -1 && selectedColumn != -1 && !this.table.isEditing()) {
  94.          boolean editing = this.table.editCellAt(selectedRow, selectedColumn);
  95.          this.table.requestFocus();
  96.          if (!editing) {
  97.             return;
  98.          }
  99.       }
  100.  
  101.       Component editorComp = this.table.getEditorComponent();
  102.       if (this.table.isEditing() && editorComp != null) {
  103.          char keyChar = e.getKeyChar();
  104.          if (editorComp instanceof JTextField) {
  105.             JTextField textField = (JTextField)editorComp;
  106.             Keymap keyMap = ((JTextComponent)textField).getKeymap();
  107.             KeyStroke key = KeyStroke.getKeyStroke(keyChar, 0);
  108.             Action action = keyMap.getAction(key);
  109.             if (action == null) {
  110.                action = keyMap.getDefaultAction();
  111.             }
  112.  
  113.             if (action != null) {
  114.                ActionEvent ae = new ActionEvent(textField, 1001, String.valueOf(keyChar));
  115.                action.actionPerformed(ae);
  116.             }
  117.          }
  118.       }
  119.  
  120.    }
  121.  
  122.    protected void drawGridInClipRect(Rectangle rect, Graphics g) {
  123.       g.setColor(this.table.getGridColor());
  124.       if (this.table.getShowHorizontalLines()) {
  125.          this.drawHorizontalLinesInClipRect(rect, g);
  126.       }
  127.  
  128.       if (this.table.getShowVerticalLines()) {
  129.          this.drawVerticalLinesInClipRect(rect, g);
  130.       }
  131.  
  132.    }
  133.  
  134.    protected void drawHorizontalLinesInClipRect(Rectangle rect, Graphics g) {
  135.       int delta = this.table.getRowHeight() + this.table.getIntercellSpacing().height;
  136.       Rectangle r = g.getClipBounds();
  137.       int firstIndex = this.table.rowAtPoint(new Point(0, r.y));
  138.       int lastIndex = this.lastVisibleRow(r);
  139.       int y = delta * firstIndex + (delta - 1);
  140.  
  141.       for(int index = firstIndex; index <= lastIndex; ++index) {
  142.          if (y >= rect.y && y <= rect.y + rect.height) {
  143.             g.drawLine(rect.x, y, rect.x + rect.width - 1, y);
  144.          }
  145.  
  146.          y += delta;
  147.       }
  148.  
  149.    }
  150.  
  151.    protected void drawRowInClipRect(int row, Rectangle rect, Graphics g) {
  152.       int column = 0;
  153.       boolean drawn = false;
  154.       Rectangle draggedCellRect = null;
  155.       int draggedColumnIndex = -1;
  156.       Enumeration enumeration = this.table.getColumnModel().getColumns();
  157.       Dimension spacing = this.table.getIntercellSpacing();
  158.       JTableHeader header = this.table.getTableHeader();
  159.       Rectangle cellRect = new Rectangle();
  160.       cellRect.height = this.table.getRowHeight() + spacing.height;
  161.  
  162.       for(cellRect.y = row * cellRect.height; enumeration.hasMoreElements(); ++column) {
  163.          TableColumn aColumn = (TableColumn)enumeration.nextElement();
  164.          cellRect.width = aColumn.getWidth() + spacing.width;
  165.          if (cellRect.intersects(rect)) {
  166.             drawn = true;
  167.             if (header != null && aColumn == header.getDraggedColumn()) {
  168.                g.setColor(this.table.getParent().getBackground());
  169.                g.fillRect(cellRect.x, cellRect.y, cellRect.width, cellRect.height);
  170.                draggedCellRect = new Rectangle(cellRect);
  171.                draggedColumnIndex = column;
  172.             } else {
  173.                TableCellRenderer renderer = this.getCellRenderer(column);
  174.                Component component = this.prepareRenderer(renderer, this.table, row, column);
  175.                this.drawWithComponent(g, component, cellRect);
  176.             }
  177.          } else if (drawn) {
  178.             break;
  179.          }
  180.  
  181.          cellRect.x += cellRect.width;
  182.       }
  183.  
  184.       if (draggedColumnIndex != -1 && draggedCellRect != null) {
  185.          TableCellRenderer var20 = this.getCellRenderer(draggedColumnIndex);
  186.          if (var20 != null) {
  187.             Component component = this.prepareRenderer(var20, this.table, row, draggedColumnIndex);
  188.             draggedCellRect.x += header.getDraggedDistance();
  189.             Color bColor = this.table.getBackground();
  190.             if (bColor != null) {
  191.                g.setColor(bColor);
  192.                g.fillRect(draggedCellRect.x, draggedCellRect.y, draggedCellRect.width, draggedCellRect.height);
  193.             }
  194.  
  195.             g.setColor(this.table.getGridColor());
  196.             int x1 = draggedCellRect.x;
  197.             int y1 = draggedCellRect.y;
  198.             int x2 = x1 + draggedCellRect.width - 1;
  199.             int y2 = y1 + draggedCellRect.height - 1;
  200.             if (this.table.getShowVerticalLines()) {
  201.                g.drawLine(x2, y1, x2, y2);
  202.             }
  203.  
  204.             if (this.table.getShowHorizontalLines()) {
  205.                g.drawLine(x1, y2, x2, y2);
  206.             }
  207.  
  208.             this.drawWithComponent(g, component, draggedCellRect);
  209.          }
  210.       }
  211.  
  212.    }
  213.  
  214.    protected void drawVerticalLinesInClipRect(Rectangle rect, Graphics g) {
  215.       int x = 0;
  216.       int count = this.table.getColumnCount();
  217.  
  218.       for(int index = 0; index <= count; ++index) {
  219.          if (x > 0 && x - 1 >= rect.x && x - 1 <= rect.x + rect.width) {
  220.             g.drawLine(x - 1, rect.y, x - 1, rect.y + rect.height - 1);
  221.          }
  222.  
  223.          if (index < count) {
  224.             x += this.table.getColumnModel().getColumn(index).getWidth() + this.table.getIntercellSpacing().width;
  225.          }
  226.       }
  227.  
  228.    }
  229.  
  230.    protected void drawWithComponent(Graphics g, Component component, Rectangle cellRect) {
  231.       Dimension spacing = this.table.getIntercellSpacing();
  232.       cellRect.setBounds(cellRect.x + spacing.width / 2, cellRect.y + spacing.height / 2, cellRect.width - spacing.width, cellRect.height - spacing.height);
  233.       if (component.getParent() == null) {
  234.          this.rendererPane.add(component);
  235.       }
  236.  
  237.       this.rendererPane.paintComponent(g, component, this.table, cellRect.x, cellRect.y, cellRect.width, cellRect.height, true);
  238.       cellRect.setBounds(cellRect.x - spacing.width / 2, cellRect.y - spacing.height / 2, cellRect.width + spacing.width, cellRect.height + spacing.height);
  239.    }
  240.  
  241.    public void focusGained(FocusEvent e) {
  242.       if (this.table.getSelectedColumn() == -1) {
  243.          this.table.setColumnSelectionInterval(0, 0);
  244.       }
  245.  
  246.       if (this.table.getSelectedRow() == -1) {
  247.          this.table.setRowSelectionInterval(0, 0);
  248.       }
  249.  
  250.       this.repaintAnchorCell();
  251.    }
  252.  
  253.    public void focusLost(FocusEvent e) {
  254.       this.repaintAnchorCell();
  255.    }
  256.  
  257.    protected TableCellRenderer getCellRenderer(int column) {
  258.       TableColumn tableColumn = this.table.getColumnModel().getColumn(column);
  259.       TableCellRenderer renderer = tableColumn.getCellRenderer();
  260.       if (renderer == null) {
  261.          Class columnClass = this.table.getColumnClass(column);
  262.          renderer = this.table.getDefaultRenderer(columnClass);
  263.       }
  264.  
  265.       return renderer;
  266.    }
  267.  
  268.    public Dimension getMaximumSize(JComponent c) {
  269.       return this.getPreferredSize(c);
  270.    }
  271.  
  272.    public Dimension getMinimumSize(JComponent c) {
  273.       return this.getPreferredSize(c);
  274.    }
  275.  
  276.    public Dimension getPreferredSize(JComponent c) {
  277.       JTable table = (JTable)c;
  278.       Dimension size = new Dimension();
  279.       int mode = table.getAutoResizeMode();
  280.       Component parent = ((Component)table).getParent();
  281.       if (mode != 0 && parent instanceof JViewport) {
  282.          size.width = parent.getBounds().width;
  283.          table.sizeColumnsToFit(mode == 1);
  284.       } else {
  285.          size.width = table.getColumnModel().getTotalColumnWidth();
  286.       }
  287.  
  288.       size.height = table.getRowCount() * (table.getRowHeight() + table.getIntercellSpacing().height);
  289.       return size;
  290.    }
  291.  
  292.    public void installUI(JComponent c) {
  293.       this.table = (JTable)c;
  294.       ((Component)c).addMouseListener(this);
  295.       ((Component)c).addMouseMotionListener(this);
  296.       ((Component)c).addFocusListener(this);
  297.       this.rendererPane = new CellRendererPane();
  298.       this.table.add(this.rendererPane);
  299.       this.configureTable();
  300.       this.registerKeyboardActions();
  301.    }
  302.  
  303.    private int lastVisibleRow(Rectangle clipRect) {
  304.       int lastIndex = this.table.rowAtPoint(new Point(0, clipRect.y + clipRect.height - 1));
  305.       if (lastIndex == -1) {
  306.          lastIndex = this.table.getRowCount() - 1;
  307.       }
  308.  
  309.       return lastIndex;
  310.    }
  311.  
  312.    public void mouseClicked(MouseEvent e) {
  313.    }
  314.  
  315.    public void mouseDragged(MouseEvent e) {
  316.       if (this.table.isEditing()) {
  317.          if (this.dispatchComponent != null) {
  318.             this.dispatchComponent.dispatchEvent(SwingUtilities.convertMouseEvent(this.table, e, this.dispatchComponent));
  319.          }
  320.  
  321.       } else {
  322.          Point p = e.getPoint();
  323.          int hitRowIndex = this.table.rowAtPoint(p);
  324.          int hitColumnIndex = this.table.columnAtPoint(p);
  325.          if (hitColumnIndex != -1 && hitRowIndex != -1) {
  326.             this.updateSelection(hitRowIndex, hitColumnIndex, false, false);
  327.          }
  328.       }
  329.    }
  330.  
  331.    public void mouseEntered(MouseEvent e) {
  332.       if (this.dispatchComponent != null) {
  333.          this.dispatchComponent = null;
  334.       }
  335.  
  336.    }
  337.  
  338.    public void mouseExited(MouseEvent e) {
  339.       if (this.dispatchComponent != null) {
  340.          this.dispatchComponent = null;
  341.       }
  342.  
  343.    }
  344.  
  345.    public void mouseMoved(MouseEvent e) {
  346.       if (this.dispatchComponent != null) {
  347.          this.dispatchComponent = null;
  348.       }
  349.  
  350.    }
  351.  
  352.    public void mousePressed(MouseEvent e) {
  353.       if (!this.phantomMousePressed) {
  354.          this.phantomMousePressed = true;
  355.          Point p = e.getPoint();
  356.          int hitRowIndex = this.table.rowAtPoint(p);
  357.          int hitColumnIndex = this.table.columnAtPoint(p);
  358.          if (hitColumnIndex != -1 && hitRowIndex != -1) {
  359.             Rectangle cellRect = this.table.getCellRect(hitRowIndex, hitColumnIndex, false);
  360.             if (cellRect.contains(p)) {
  361.                this.table.editCellAt(hitRowIndex, hitColumnIndex, e);
  362.             }
  363.  
  364.             if (!this.table.isEditing()) {
  365.                this.table.requestFocus();
  366.                boolean controlKeyDown = ((InputEvent)e).isControlDown();
  367.                boolean shiftKeyDown = ((InputEvent)e).isShiftDown();
  368.                boolean deselect = controlKeyDown && this.table.isCellSelected(hitRowIndex, hitColumnIndex);
  369.                if (!controlKeyDown && !shiftKeyDown) {
  370.                   this.clearSelection();
  371.                }
  372.  
  373.                this.updateSelection(hitRowIndex, hitColumnIndex, deselect, !shiftKeyDown || deselect);
  374.             } else {
  375.                Component editorComp = this.table.getEditorComponent();
  376.                Point componentPoint = SwingUtilities.convertPoint(this.table, new Point(e.getX(), e.getY()), editorComp);
  377.                this.dispatchComponent = SwingUtilities.getDeepestComponentAt(editorComp, componentPoint.x, componentPoint.y);
  378.                this.dispatchComponent.dispatchEvent(SwingUtilities.convertMouseEvent(this.table, e, this.dispatchComponent));
  379.             }
  380.  
  381.          }
  382.       }
  383.    }
  384.  
  385.    public void mouseReleased(MouseEvent e) {
  386.       this.phantomMousePressed = false;
  387.       if (this.table.isEditing()) {
  388.          if (this.dispatchComponent != null) {
  389.             this.dispatchComponent.dispatchEvent(SwingUtilities.convertMouseEvent(this.table, e, this.dispatchComponent));
  390.          }
  391.  
  392.          this.dispatchComponent = null;
  393.       }
  394.    }
  395.  
  396.    private void moveAnchor(int row, int column) {
  397.       if (this.table.isEditing()) {
  398.          TableCellEditor cellEditor = this.table.getCellEditor();
  399.          if (cellEditor != null) {
  400.             boolean stopped = cellEditor.stopCellEditing();
  401.             if (!stopped) {
  402.                return;
  403.             }
  404.          }
  405.       }
  406.  
  407.       this.updateSelection(row, column, false, true);
  408.    }
  409.  
  410.    public void paint(Graphics g, JComponent c) {
  411.       JTable table = (JTable)c;
  412.       Rectangle paintBounds = g.getClipBounds();
  413.       ((Component)table).getSize();
  414.       if (table.getColumnModel() != null) {
  415.          Dimension pSize = this.getPreferredSize(table);
  416.          Rectangle tableRect = new Rectangle(0, 0, pSize.width, pSize.height);
  417.          tableRect = tableRect.intersection(paintBounds);
  418.          g.setColor(((Component)table).getParent().getBackground());
  419.          g.fillRect(paintBounds.x, paintBounds.y, paintBounds.width, paintBounds.height);
  420.          if (tableRect.height >= 0 && tableRect.width >= 0) {
  421.             Color bColor = ((Component)table).getBackground();
  422.             if (bColor != null) {
  423.                g.setColor(bColor);
  424.                g.fillRect(tableRect.x, tableRect.y, tableRect.width, tableRect.height);
  425.             }
  426.  
  427.             this.drawGridInClipRect(tableRect, g);
  428.             Rectangle r = g.getClipBounds();
  429.             int firstIndex = table.rowAtPoint(new Point(0, r.y));
  430.             int lastIndex = this.lastVisibleRow(r);
  431.             Rectangle rowRect = new Rectangle(0, 0, table.getColumnModel().getTotalColumnWidth(), table.getRowHeight() + table.getIntercellSpacing().height);
  432.             rowRect.y = firstIndex * rowRect.height;
  433.  
  434.             for(int index = firstIndex; index <= lastIndex; ++index) {
  435.                if (rowRect.intersects(tableRect)) {
  436.                   Rectangle intersection = rowRect.intersection(tableRect);
  437.                   this.drawRowInClipRect(index, intersection, g);
  438.                }
  439.  
  440.                rowRect.y += rowRect.height;
  441.             }
  442.  
  443.          }
  444.       }
  445.    }
  446.  
  447.    protected Component prepareRenderer(TableCellRenderer renderer, JTable table, int row, int column) {
  448.       Object value = table.getValueAt(row, column);
  449.       boolean isSelected = table.isCellSelected(row, column);
  450.       boolean rowIsAnchor = table.getSelectedRow() == row;
  451.       boolean columnIsAnchor = table.getSelectedColumn() == column;
  452.       boolean hasFocus = rowIsAnchor && columnIsAnchor && ((JComponent)table).hasFocus();
  453.       return renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
  454.    }
  455.  
  456.    private void registerArrowKey(int keyEvent, int dx, int dy) {
  457.       this.table.registerKeyboardAction(new ArrowKeyAction(this, dx, dy), KeyStroke.getKeyStroke(keyEvent, 0), 0);
  458.    }
  459.  
  460.    private void registerKeyboardActions() {
  461.       this.registerArrowKey(39, 1, 0);
  462.       this.registerArrowKey(37, -1, 0);
  463.       this.registerArrowKey(38, 0, -1);
  464.       this.registerArrowKey(40, 0, 1);
  465.       this.tableKeyListener = new TableKeyListener(this);
  466.       this.table.addKeyListener(this.tableKeyListener);
  467.    }
  468.  
  469.    private void repaintAnchorCell() {
  470.       int anchorRow = this.table.getSelectedRow();
  471.       int anchorColumn = this.table.getSelectedColumn();
  472.       Rectangle dirtyRect = this.table.getCellRect(anchorRow, anchorColumn, false);
  473.       this.table.repaint(dirtyRect);
  474.    }
  475.  
  476.    public void uninstallUI(JComponent c) {
  477.       this.table.remove(this.rendererPane);
  478.       ((Component)c).removeMouseListener(this);
  479.       ((Component)c).removeMouseMotionListener(this);
  480.       ((Component)c).removeFocusListener(this);
  481.       this.unregisterKeyboardActions();
  482.       this.rendererPane = null;
  483.       this.table = null;
  484.    }
  485.  
  486.    private void unregisterKeyboardActions() {
  487.       this.table.unregisterKeyboardAction(KeyStroke.getKeyStroke(38, 0));
  488.       this.table.unregisterKeyboardAction(KeyStroke.getKeyStroke(40, 0));
  489.       this.table.unregisterKeyboardAction(KeyStroke.getKeyStroke(39, 0));
  490.       this.table.unregisterKeyboardAction(KeyStroke.getKeyStroke(37, 0));
  491.       this.table.removeKeyListener(this.tableKeyListener);
  492.    }
  493.  
  494.    private void updateSelection(int rowIndex, int columnIndex, boolean deselect, boolean reAnchor) {
  495.       Rectangle cellRect = this.table.getCellRect(rowIndex, columnIndex, false);
  496.       if (cellRect != null) {
  497.          this.table.scrollRectToVisible(cellRect);
  498.       }
  499.  
  500.       ListSelectionModel rsm = this.table.getSelectionModel();
  501.       ListSelectionModel csm = this.table.getColumnModel().getSelectionModel();
  502.       this.updateSelectionModel(csm, columnIndex, deselect, reAnchor);
  503.       this.updateSelectionModel(rsm, rowIndex, deselect, reAnchor);
  504.    }
  505.  
  506.    private void updateSelectionModel(ListSelectionModel sm, int index, boolean deselect, boolean reAnchor) {
  507.       if (reAnchor) {
  508.          if (deselect) {
  509.             sm.removeSelectionInterval(index, index);
  510.             return;
  511.          }
  512.  
  513.          sm.addSelectionInterval(index, index);
  514.       }
  515.  
  516.       sm.setLeadSelectionIndex(index);
  517.    }
  518.  
  519.    static void access$clearSelection(BasicTableUI var0, ListSelectionModel var1) {
  520.       var0.clearSelection(var1);
  521.    }
  522.  
  523.    static void access$moveAnchor(BasicTableUI var0, int var1, int var2) {
  524.       var0.moveAnchor(var1, var2);
  525.    }
  526.  
  527.    static void access$dispatchKeyboardEvent(BasicTableUI var0, KeyEvent var1) {
  528.       var0.dispatchKeyboardEvent(var1);
  529.    }
  530. }
  531.